<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>微博三方登录 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="微博三方登录流程 vue端发送post请求，获取微博的扫码URL django端返回微博的URL vue通过URL跳转到微博的扫码页面 微博端扫码后，跳转的vue端那个，redirect_uri的地址上，并且会携带一个code码，通过这个code码就可以到微博中查询到扫码人的微博信息 vue端把这个code交给了django django端获取到code，并使用code到微博点查询用户信息 如果用">
<meta property="og:type" content="article">
<meta property="og:title" content="微博三方登录">
<meta property="og:url" content="https://mourning-soul.gitee.io/hexo/2021/01/28/%E5%BE%AE%E5%8D%9A%E7%AC%AC%E4%B8%89%E6%96%B9%E7%99%BB%E5%BD%95/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="微博三方登录流程 vue端发送post请求，获取微博的扫码URL django端返回微博的URL vue通过URL跳转到微博的扫码页面 微博端扫码后，跳转的vue端那个，redirect_uri的地址上，并且会携带一个code码，通过这个code码就可以到微博中查询到扫码人的微博信息 vue端把这个code交给了django django端获取到code，并使用code到微博点查询用户信息 如果用">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-01-27T16:00:00.000Z">
<meta property="article:modified_time" content="2021-01-28T05:56:21.774Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="-blocker">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/hexo/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/hexo/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/hexo/css/style.css">

  
    
<link rel="stylesheet" href="/hexo/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/hexo/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/hexo/">Home</a>
        
          <a class="main-nav-link" href="/hexo/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/hexo/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://mourning-soul.gitee.io/hexo"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-微博第三方登录" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/hexo/2021/01/28/%E5%BE%AE%E5%8D%9A%E7%AC%AC%E4%B8%89%E6%96%B9%E7%99%BB%E5%BD%95/" class="article-date">
  <time class="dt-published" datetime="2021-01-27T16:00:00.000Z" itemprop="datePublished">2021-01-28</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      微博三方登录
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h2 id="微博三方登录流程"><a href="#微博三方登录流程" class="headerlink" title="微博三方登录流程"></a>微博三方登录流程</h2><ul>
<li>vue端发送post请求，获取微博的扫码URL</li>
<li>django端返回微博的URL</li>
<li>vue通过URL跳转到微博的扫码页面</li>
<li>微博端扫码后，跳转的vue端那个，redirect_uri的地址上，并且会携带一个code码，通过这个code码就可以到微博中查询到扫码人的微博信息</li>
<li>vue端把这个code交给了django</li>
<li>django端获取到code，并使用code到微博点查询用户信息</li>
<li>如果用户已经帮点过，直接登录成功，如果用户没有绑定过，返回微博的用户ID</li>
<li>如果返回的type=0，用户已经绑定过，直接跳转到首页，如果返回的type!=0，携带微博UID跳转到绑定页面</li>
<li>在vue绑定页面中，发送用户名(username),密码(password),微博UID到django后端</li>
<li>如果用户存在，判断用户名和面试否正确，正确绑定，错误提示前端，如果用户名不存在，创建用户，并添加绑定关系</li>
</ul>
<br>

<h2 id="操作步骤"><a href="#操作步骤" class="headerlink" title="操作步骤"></a>操作步骤</h2><h3 id="注册微博开放平台"><a href="#注册微博开放平台" class="headerlink" title="注册微博开放平台"></a>注册微博开放平台</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">选择微链接-&gt;网站接入&#x3D;&gt;立即接入 新建一个应用</span><br></pre></td></tr></table></figure>
<h3 id="拿到appkey和appsecret-然后立即点击高级地址，填写回调地址"><a href="#拿到appkey和appsecret-然后立即点击高级地址，填写回调地址" class="headerlink" title="拿到appkey和appsecret,然后立即点击高级地址，填写回调地址"></a>拿到appkey和appsecret,然后立即点击高级地址，填写回调地址</h3><h4 id="注意：回调授权和取消回调授权地址需要一致"><a href="#注意：回调授权和取消回调授权地址需要一致" class="headerlink" title="注意：回调授权和取消回调授权地址需要一致"></a>注意：回调授权和取消回调授权地址需要一致</h4><br>

<h4 id="那么新浪微博的登录逻辑很简单：-拼接url-gt-回调获取code-gt-利用code换取uid和access-token-gt-使用access-token获取用户信息"><a href="#那么新浪微博的登录逻辑很简单：-拼接url-gt-回调获取code-gt-利用code换取uid和access-token-gt-使用access-token获取用户信息" class="headerlink" title="那么新浪微博的登录逻辑很简单： 拼接url =&gt; 回调获取code =&gt; 利用code换取uid和access_token =&gt; 使用access_token获取用户信息"></a>那么新浪微博的登录逻辑很简单： 拼接url =&gt; 回调获取code =&gt; 利用code换取uid和access_token =&gt; 使用access_token获取用户信息</h4><hr>
<br>

<h3 id="后端"><a href="#后端" class="headerlink" title="后端"></a>后端</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"># 定义微博回调视图</span><br><span class="line">def wb_back(request):</span><br><span class="line">    # 获取回调的code</span><br><span class="line">    code &#x3D; request.GET.get(&#39;code&#39;)</span><br><span class="line">    # 定义认证地址</span><br><span class="line">    access_token_url &#x3D; &#39;https:&#x2F;&#x2F;api.weibo.com&#x2F;oauth2&#x2F;access_token&#39;</span><br><span class="line">    # 发起请求，交换sina的id</span><br><span class="line">    re_dict &#x3D; requests.post(</span><br><span class="line">        access_token_url,</span><br><span class="line">        data&#x3D;&#123;</span><br><span class="line">            &quot;client_id&quot;: &#39;2636039333&#39;,  # 应用id</span><br><span class="line">            &quot;client_secret&quot;: &quot;*************************&quot;,  # 密钥</span><br><span class="line">            &quot;grant_type&quot;: &quot;authorization_code&quot;,  # 授权方式</span><br><span class="line">            &quot;code&quot;: code,  # code</span><br><span class="line">            &quot;redirect_uri&quot;: &quot;http:&#x2F;&#x2F;127.0.0.1:8000&#x2F;weibo_back&quot;  # 回调网址</span><br><span class="line">        &#125;</span><br><span class="line">    )</span><br><span class="line">    # 获取返回值</span><br><span class="line">    re_dict &#x3D; re_dict.text</span><br><span class="line">    # 强转</span><br><span class="line">    re_dict &#x3D; eval(re_dict)</span><br><span class="line">    print(re_dict)</span><br><span class="line">    # 判断是否为第一次登陆</span><br><span class="line">    user &#x3D; User.objects.filter(username&#x3D;str(re_dict[&#39;uid&#39;])).first()</span><br><span class="line">    if user:</span><br><span class="line">        # 代表曾经用过新浪登录</span><br><span class="line">        sina_id &#x3D; user.username</span><br><span class="line">        user_id &#x3D; user.id</span><br><span class="line">    else:</span><br><span class="line">        # 代表首次登陆</span><br><span class="line">        user &#x3D; User(username&#x3D;str(re_dict[&#39;uid&#39;]),password&#x3D;&#39;&#39;)</span><br><span class="line">        # 保存入库</span><br><span class="line">        user.save()</span><br><span class="line">        sina_id &#x3D; str(re_dict[&#39;uid&#39;])</span><br><span class="line">        # 查询用户id</span><br><span class="line">        user &#x3D; User.objects.filter(username&#x3D;str(re_dict[&#39;uid&#39;])).first()</span><br><span class="line">        user_id &#x3D; user.id</span><br><span class="line">    #进行跳转</span><br><span class="line">    return redirect(&#39;http:&#x2F;&#x2F;localhost:8080?sina_id&#x3D;&#39;+str(sina_id)+&#39;&amp;uid&#x3D;&#39;+str(user_id))</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<hr>
<br>

<h3 id="前端"><a href="#前端" class="headerlink" title="前端"></a>前端</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 跳转到新浪微博</span><br><span class="line">    sinalogin:function()&#123;</span><br><span class="line">      &#x2F;&#x2F; 应用id client_id&#x3D;2636039333</span><br><span class="line">      &#x2F;&#x2F;redirect_url： 回调网址</span><br><span class="line"></span><br><span class="line">      &#x2F;&#x2F; 拼接新浪url</span><br><span class="line">      let url &#x3D; &quot;https:&#x2F;&#x2F;api.weibo.com&#x2F;oauth2&#x2F;authorize?client_id&#x3D;2636039333&amp;redirect_uri&#x3D;http:&#x2F;&#x2F;127.0.0.1:8000&#x2F;weibo&quot;;</span><br><span class="line">      &#x2F;&#x2F; 进行站外跳转</span><br><span class="line">      window.location.href &#x3D; url;</span><br><span class="line"></span><br><span class="line">    &#125;,</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="https://mourning-soul.gitee.io/hexo/2021/01/28/%E5%BE%AE%E5%8D%9A%E7%AC%AC%E4%B8%89%E6%96%B9%E7%99%BB%E5%BD%95/" data-id="ckkt9opg2000gi4tweouo74xq" data-title="微博三方登录" class="article-share-link">Share</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/hexo/tags/blocker/" rel="tag">-blocker</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/hexo/2021/01/29/%E4%B8%89%E6%96%B9%E6%94%AF%E4%BB%98/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          三方支付
        
      </div>
    </a>
  
  
    <a href="/hexo/2021/01/27/vue%E7%88%B6%E5%AD%90%E7%BB%84%E4%BB%B6%E3%80%81%E5%AD%90%E7%88%B6%E7%BB%84%E4%BB%B6/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">vue父子组件、子父组件</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/blocker/" rel="tag">-blocker</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/hexo/tags/blocker/" style="font-size: 10px;">-blocker</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/02/">February 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/01/">January 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/hexo/2021/02/05/%E8%A7%86%E9%A2%91%E7%9B%B4%E6%92%AD/">视频直播</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/04/%E6%97%A0%E9%99%90%E6%9E%81%E5%88%86%E7%B1%BB%E8%87%AA%E5%85%B3%E8%81%94%E9%80%92%E5%BD%92/">无限极分类自关联递归</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/03/webshell/">webshell</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/02/redis%E9%94%AE%E7%A9%BA%E9%97%B4%E4%BA%8B%E4%BB%B6/">redis键空间事件</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/01/redis%E5%8F%91%E5%B8%83%E8%80%85%E8%AE%A2%E9%98%85%E8%80%85/">redis发布者订阅者</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/hexo/" class="mobile-nav-link">Home</a>
  
    <a href="/hexo/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/hexo/js/jquery-3.4.1.min.js"></script>



  
<script src="/hexo/fancybox/jquery.fancybox.min.js"></script>




<script src="/hexo/js/script.js"></script>





  </div>
</body>
</html>